import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		pageIndex: 1,
		pageItems: 20,
		totalPages: 1,
		userList: [],
		vshow: false, //是否显示 
		inputValue: '',
		showContent: true,
		showData: false,
	},
	onLoad(params) {
		this.loadUserList();
	},

	//渲染加载
	loadUserList: function(e) {
		var newUrl = getApp().globalData.dangjianUrl + "/member/partyMember";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params["test"] = "";
		params["userName"] = "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "人员列表加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var members = respData.data.members;
					var totalPages = members.totalPages;
					members = members.members;
					var oldUserList = this.data.userList;
					var userList = [];
					for (var index in members) {
						var user = members[index];
						var iconUrl = user.iconUrl;
						if (null == iconUrl || '' == iconUrl) {
							iconUrl = "../../img/headICON.png";
						}
						var userId = user.userId;
						if (null == userId) {
							userId = -1;
						}
						user["userId"] = userId;
						user["iconUrl"] = iconUrl;
						userList.push(user);
					}
					userList = oldUserList.concat(userList);
					this.setData({
						"userList": userList,
						"totalPages": totalPages
					});
        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},

	//点击图标 搜索事件
	searching(event) {
		if (this.data.inputValue != "") {
			dd.navigateTo({
				url: '../search/search?assetId=123&folderId=456'
			})
		}
	},
  //点击键盘完成 搜索
  selectResult: function (e) {
    // console.log('select result', e.detail)
    var newUrl = getApp().globalData.dangjianUrl + "/member/partyMember";
    var params = {};
    params["pageIndex"] = this.data.pageIndex + "";
    params["pageItems"] = this.data.pageItems + "";
    params["test"] = "";
    params["userName"] = e.detail.value + "";
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "成员搜索加载失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          if (respData.data.members.items > 0) {
            var members = respData.data.members;
            var totalPages = members.totalPages;
            members = members.members;
            var oldUserList = this.data.userList;
            var userList = [];
            for (var index in members) {
              var user = members[index];
              var iconUrl = user.iconUrl;
              if (null == iconUrl || '' == iconUrl) {
                iconUrl = "../../img/headICON.png";
              }
              var userId = user.userId;
              if (null == userId) {
                userId = -1;
              }
              user["userId"] = userId;
              user["iconUrl"] = iconUrl;
              userList.push(user);
            }
            // userList = oldUserList.concat(userList);
            this.setData({
              "userList": userList,
              "totalPages": totalPages,
              showContent: true,
              showData: false,
            });
          } else {
            this.setData({
              showContent: false,
              showData: true,
            })
          }

        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
      }
    );
		// dd.navigateTo({
		// 	url: '../search/search?assetId=123&folderId=456'
		// })
  },
	//点击键盘完成 搜索
	searchSubmit(event) {
		var newUrl = getApp().globalData.dangjianUrl + "/member/partyMember";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params["test"] = "";
		params["userName"] = this.data.inputValue + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "成员搜索加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					if (respData.data.members.items > 0) {
						var members = respData.data.members;
						var totalPages = members.totalPages;
						members = members.members;
						var oldUserList = this.data.userList;
						var userList = [];
						for (var index in members) {
							var user = members[index];
							var iconUrl = user.iconUrl;
							if (null == iconUrl || '' == iconUrl) {
								iconUrl = "../../img/headICON.png";
							}
							var userId = user.userId;
							if (null == userId) {
								userId = -1;
							}
							user["userId"] = userId;
							user["iconUrl"] = iconUrl;
							userList.push(user);
						}
						// userList = oldUserList.concat(userList);
						this.setData({
							"userList": userList,
							"totalPages": totalPages,
							showContent: true,
							showData: false,
						});
					} else {
						this.setData({
							showContent: false,
							showData: true,
						})
					}

        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
		// dd.navigateTo({
		// 	url: '../search/search?assetId=123&folderId=456'
		// })
	},

	//按下键盘事件
	searchVlues(event) {
		//当按下的值不为空，调出 clear 图标
		if (event.detail.value != "") {
			this.setData({
				vshow: true,
				inputValue: event.detail.value,
			})
		} else {
			this.setData({
				vshow: false
			})
		}
	},

	//按下删除标识 清空输入框
	clearEvent(event) {
		console.log(this.data.inputValue + 1234)
		this.setData({
			inputValue: "",
			vshow: false,
		})
	},

	//触底加载
	onReachBottom: function(e) {
		var pageIndex = this.data.pageIndex + 1;
		if (pageIndex <= this.data.totalPages) {
			this.setData({
				"pageIndex": pageIndex
			});
			this.loadUserList();
		} else {
			this.showPageMsg("tips", "没有更多数据了");
		}
	},

	//下拉刷新
	onPullDownRefresh: function(e) {
		this.setData({
			pageIndex: 1,
			userList: []
		});
		this.loadUserList();
		dd.stopPullDownRefresh();
	}


});
